package laMalinche.testingPlayGround.swingTest;

import laMalinche.BasicConstraintLaMa;
import laMalinche.LaMalinche;
import laMalinche.LaMaContext;
import laMalinche.propertyChangeAdapters.model.AccessReporterImp;
import laMalinche.testingPlayGround.flow.BasicFlowNode;
import laMalinche.testingPlayGround.flow.Collector;
import laMalinche.testingPlayGround.flow.FlowHolder;
import laMalinche.testingPlayGround.flow.Navigator;
import laMalinche.testingPlayGround.testModel.PersonVo;
import java.beans.PropertyChangeListener;

/**
 * Created by IntelliJ IDEA.
 * User: ran_tene
 * Date: Sep 3, 2008
 * Time: 12:10:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class SwingTest {
    private PersonVo person;

    public static void main(String[] args) {
        final SwingTest swingTest = new SwingTest();
        AccessReporterImp listener = new AccessReporterImp();
        PersonVo personVo = swingTest.makeModel(listener);
        FlowHolder holder = swingTest.initializeFlow(personVo);
        LaMalinche laMalinche = swingTest.makeLaMa(holder, listener);
        FamilyEditor familyEditor = new FamilyEditor(laMalinche, holder);
        laMalinche.start();
        laMalinche.updateConstraintStatus();
        familyEditor.initializeGuy();
    }

    private FlowHolder initializeFlow(final PersonVo person) {
        BasicFlowNode node = new BasicFlowNode(new Collector(person, "age"));
        node.addNavigator(new Navigator() {
            public Collector navigate(Collector collector) {
                PersonVo vo;
                if (person.getAge() > 5) {
                    vo = person.getFather();
                } else {
                    vo = person.getMother();
                }
                return new Collector(vo, "age");
            }
        });
        return  new FlowHolder(node);
    }

    protected LaMalinche makeLaMa(FlowHolder swingFlow, AccessReporterImp accessReporterImp) {
        LaMalinche laMalinche = new BasicConstraintLaMa();
        LaMaContext.getInstance().setAccessReporter(accessReporterImp);
        laMalinche.setAboutToChangeManager(swingFlow.getAboutToChangeManager());
        laMalinche.add(SwingTestRules.createteProductionRules(person));
        LaMaContext.getInstance().setAccessReporter(accessReporterImp);
        return laMalinche;
    }

    protected PersonVo makeModel(PropertyChangeListener propertyChangeListener) {
        person = new PersonVo();
        person.setAge(5);
        person.setAccessReporter(propertyChangeListener);
        final PersonVo mother = new PersonVo();
        mother.setAge(2);
        person.setMother(mother);
        final PersonVo father = new PersonVo();
        person.setFather(father);
        father.setAge(3);
        return person;
    }


}
